public class liko {
    class Solution {
        public int takeCharacters(String s, int k) {
            int aCount = 0, bCount = 0, cCount = 0;
            int n = s.length();
            for (int i = 0; i < n; i++) {
                if (s.charAt(i) == 'a') {
                    aCount++;
                } else if (s.charAt(i) == 'b') {
                    bCount++;
                } else {
                    cCount++;
                }
            }

            if (aCount < k || bCount < k || cCount < k) {
                return -1;
            }

            int aLeft = aCount - k, bLeft = bCount - k, cLeft = cCount - k;
            int left = 0, right = 0;
            int[] arr = new int[3];
            while (right < n) {
                arr[s.charAt(right) - 'a']++;
                if (arr[0] > aLeft || arr[1] > bLeft || arr[2] > cLeft) {
                    arr[s.charAt(left) - 'a']--;
                    left++;
                }
                right++;
            }
            return n - right + left;
        }
    }


}
